package com.safeluck.scannerqcr.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.safeluck.scannerqcr.R;
import com.safeluck.scannerqcr.bean.Chat;
import com.safeluck.scannerqcr.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * ScannerQCR
 *
 * @author lzw
 * @date 2017/12/1
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class ChatAdapter extends RecyclerView.Adapter <ChatAdapter.MyHolder>{

    private LayoutInflater mInflater;
    private List<Chat> mItems = new ArrayList<>();
    private OnItemClickListener mListener;

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_wechat_chat,parent,false);
        final MyHolder holder = new MyHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onItemClick(holder.getAdapterPosition(),v,holder);
                }
            }
        });
        return holder;
    }

    public void setOnItemClickListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        Chat item = mItems.get(position);
        holder.tvMsg.setText(item.message);
        holder.tvName.setText(item.name);
        holder.tvTime.setText("前天");
    }

    public ChatAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void setDatas(List<Chat> ites){
        this.mItems.clear();
        this.mItems.addAll(ites);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public Chat getItem(int position) {
        return mItems.get(position);
    }

    class MyHolder extends RecyclerView.ViewHolder{

        private ImageView imgAvatar;
        private TextView tvName,tvMsg,tvTime;

        public MyHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvMsg = itemView.findViewById(R.id.tv_msg);
        }
    }
}
